package org.cytoscape.myapp.cytoscape_app_v1.internal.tasks.visual;

import java.util.HashSet;
import java.util.Set;

import org.cytoscape.application.CyApplicationManager;
import org.cytoscape.model.CyNode;
import org.cytoscape.myapp.cytoscape_app_v1.internal.layouts.KKLayout.algorithms.KKNodeLayout;
import org.cytoscape.myapp.cytoscape_app_v1.internal.layouts.KKLayout.algorithms.KKNodeLayoutContext;
import org.cytoscape.myapp.cytoscape_app_v1.internal.metrics.Facade;
import org.cytoscape.myapp.cytoscape_app_v1.internal.model.InfoVisualOption;
import org.cytoscape.myapp.cytoscape_app_v1.internal.tasks.metrics.CalculateSocialMetricTaskFactory;
import org.cytoscape.myapp.cytoscape_app_v1.internal.visual.VisualStyleDefault;
import org.cytoscape.view.model.CyNetworkView;
import org.cytoscape.view.model.View;
import org.cytoscape.work.TaskFactory;
import org.cytoscape.work.TaskIterator;

public class VisualTaskFactory implements TaskFactory{


	private CyApplicationManager applicationManager;
	private TaskIterator taskIteratorMetrics;
	private KKNodeLayout kkNodeLayout;
	private InfoVisualOption infoVisualOption;
	private VisualStyleDefault styleDefault;
	
	
	public VisualTaskFactory(CyApplicationManager applicationManager, 
							TaskIterator taskIteratorMetrics,
							KKNodeLayout kkNodeLayout,
							InfoVisualOption infoVisualOption,
							VisualStyleDefault styleDefault) {
	
		this.applicationManager = applicationManager;
		this.taskIteratorMetrics = taskIteratorMetrics;
		this.kkNodeLayout = kkNodeLayout;
		this.infoVisualOption = infoVisualOption;
		this.styleDefault = styleDefault;
	}
	
	
	public TaskIterator createTaskIterator() {
		
		TaskIterator taskIterator = new TaskIterator();
		
		CyNetworkView netView = applicationManager.getCurrentNetworkView();
		Set<View<CyNode>> nodeViews = new HashSet<View<CyNode>>();
		for (CyNode node: applicationManager.getCurrentNetwork().getNodeList()) 
					nodeViews.add(netView.getNodeView(node));
				
		KKNodeLayoutContext context = (KKNodeLayoutContext) kkNodeLayout.getDefaultLayoutContext();
		
		TaskIterator iteratorLayout = kkNodeLayout.createTaskIterator(applicationManager.getCurrentNetworkView(), 
				context,nodeViews, this.infoVisualOption.getEdgeLenght());
		
		VisualStyleTask styleTask = new VisualStyleTask(styleDefault, infoVisualOption);
		
		taskIterator.append(this.taskIteratorMetrics);
		taskIterator.append(styleTask);
		taskIterator.append(iteratorLayout);
		
		
		return taskIterator;
	}

	
	public boolean isReady() {
	
		return false;
	}

}
